<template>
  <div class="component-upload-image" style="display: flex">
    <el-upload
      action=""
      accept="image/*"
      list-type="picture-card"
      :on-success="handleUploadSuccess"
      :before-upload="handleBeforeUpload"
      :on-error="handleUploadError"
      :http-request="uploadOSS"
      name="file"
      :show-file-list="false"
      :headers="headers"
      style="display: inline-block; vertical-align: top"
    >
      <div v-if="multiple === false">
        <el-image v-if="!files" :src="files">
          <div slot="error" class="image-slot">
            <i class="el-icon-plus"/>
          </div>
        </el-image>
        <div v-if="files" class="image">
          <el-image :src="files" :style="`width:150px;height:150px;`" fit="fill"/>
          <div class="mask">
            <div class="actions">
            <span title="预览" @click.stop="previewImage(files)">
              <i class="el-icon-zoom-in"/>
            </span>
              <span title="移除" @click.stop="removeImage">
              <i class="el-icon-delete"/>
            </span>
            </div>
          </div>
        </div>
      </div>
      <div v-if="multiple === true">
        <el-image src="''">
          <div slot="error" class="image-slot">
            <i class="el-icon-plus"/>
          </div>
        </el-image>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
      <img :src="value" style="display: block; max-width: 100%; margin: 0 auto;" alt="">
    </el-dialog>
    <div v-if="multiple === true" style="display: flex; flex-wrap: wrap; align-items: center">
      <div v-if="files && files.length > 0" class="image"  v-for="(item, index) in files" :key="index">
        <el-image  :src="item" :style="`width:150px;height:150px;margin-left:12px`"
                   fit="fill"/>
        <div class="mask">
          <div class="actions">
            <span title="预览" @click.stop="previewImage(item)">
              <i class="el-icon-zoom-in"/>
            </span>
            <span title="移除" @click.stop="removeImage(index)">
              <i class="el-icon-delete"/>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {getToken} from "@/utils/auth";
import {uploadFile} from "@/utils/miniooss";

export default {
  data() {
    return {
      dialogVisible: false,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      value: '',
      files: null,
    };
  },
  props: {
    fileList: null,
    multiple: {
      type: Boolean,
      default: false
    },
    onChange: {
      type: Function
    },
  },
  methods: {
    removeImage(index) {
      if (this.multiple === true) {
        this.files.splice(index, 1)
      } else {
        this.files = null
      }
      // this.$emit("input", this.fileList);
      this.onChange(this.files)
      // this.onChange()
    },
    previewImage(url) {
      this.dialogVisible = true
      this.value = url
    },
    handleUploadSuccess(res) {
      var imgUrl = res.url;
      if (imgUrl.indexOf("?") > -1) {
        const adUrl = imgUrl.split("?");
        if (adUrl.length > 0) {
          imgUrl = adUrl[0]
        }
      }
      if (this.multiple === true) {
        if (this.files) {
          this.files.push(imgUrl)
        } else {
          this.files = []
          this.files.push(imgUrl)
        }
      } else {
        this.files = imgUrl
      }
      // this.$emit("input", this.fileList);
      this.onChange(this.files)
      this.loading.close();
    },
    handleBeforeUpload() {
      console.log("multiple", this.multiple)
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },

    uploadOSS(file) {
      const _this = this;
      uploadFile(file.file, error => {
        console.log(error)
        this.handleUploadError()
      }, success => {
        console.log(success)
        if (success && success.url) {
          this.handleUploadSuccess(success)
        }
      }, process => {
        console.log(process)
      })
    },
  },
  watch: {
    fileList: {
      immediate: true,
      handler(fileList) {
        if (fileList) {
          this.files = fileList
        } else {
          this.files = ''
        }
        console.log("fileList", fileList)
      }
    }
  },
};
</script>

<style scoped lang="scss">
.image {
  position: relative;

  .mask {
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    transition: all 0.3s;
  }

  &:hover .mask {
    opacity: 1;
  }
}
</style>
